/*
 *  vovkulaka - printer configuration management
 *
 *  Copyright (C) 2011 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
 *
 *	This file is part of vovkulaka.
 *
 *  vovkulaka is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  vovkulaka is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with vovkulaka.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "configuration.h"

/**   C'tor of the object

    * @return initialized object
*/
CConfiguration::CConfiguration(TCHAR * szFilePath) : m_bNoConfig(false)
{
	memset(m_szConfigFile, 0, sizeof(m_szConfigFile));
	memset(m_szPrinterName, 0, sizeof(m_szPrinterName));

	TCHAR * pLastSlash = _tcsrchr(szFilePath, '\\');

	if (pLastSlash)
	{
		pLastSlash++;
		*pLastSlash = '\000';
	}

	_tcscpy_s(m_szConfigFile, _countof(m_szConfigFile), szFilePath);
	_tcscat_s(m_szConfigFile, _countof(m_szConfigFile), TEXT("vovkulaka.conf"));

	if (PathFileExists(m_szConfigFile) == FALSE)
	{
		/// no conf file ... let's create it and fill configuration
		m_bNoConfig = true;
	}
	else
	{
		TCHAR szAttributes[MAX_PATH] = {0};

		GetPrivateProfileString(SECTION_PRINTERS, TEXT("Name"), NULL, m_szPrinterName, sizeof(m_szPrinterName) / sizeof(TCHAR), m_szConfigFile);
		GetPrivateProfileString(SECTION_PRINTERS, TEXT("Attributes"), NULL, szAttributes, sizeof(szAttributes) / sizeof(TCHAR), m_szConfigFile);

		m_dwAttributes = _ttoi(szAttributes);
	}
}

/**   D'tor of the object

    * @return de-initialized object
*/
CConfiguration::~CConfiguration(void)
{
}

/**   Boolean property indicating absence of config file

    * @return boolean
*/
bool CConfiguration::getNoConfig(void)
{
	return m_bNoConfig;
}

/**   Property which set's the printer name

    * @return boolean
*/
bool CConfiguration::setPrinterName(TCHAR * szName)
{
	_tcscpy_s(m_szPrinterName, _countof(m_szPrinterName), szName);

	return true;
}

/**   Property which set's the printer attributes

    * @return boolean
*/
bool CConfiguration::setPrinterAttributes(DWORD dwAttrib)
{
	m_dwAttributes = dwAttrib;

	return true;
}

/**   Flushing values into the config file

    * @return boolean
*/
bool CConfiguration::Flush()
{
	TCHAR szAttributes[MAX_PATH] = {0};

	WritePrivateProfileString(SECTION_PRINTERS, TEXT("Name"), m_szPrinterName, m_szConfigFile);	

	_itot_s(m_dwAttributes, szAttributes, _countof(szAttributes), 10);

	WritePrivateProfileString(SECTION_PRINTERS, TEXT("Attributes"), szAttributes, m_szConfigFile);

	return true;
}

/**   Property which get's the printer name

    * @return boolean
*/
TCHAR * CConfiguration::getPrinterName()
{
	return m_szPrinterName;
}

/**   Property which get's the printer attributes

    * @return boolean
*/
DWORD CConfiguration::getPrinterAttributes()
{
	return m_dwAttributes;
}